package com.liuyk.commonlib.widget.contain;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import com.liuyk.commonlib.R;
import com.liuyk.commonlib.utility.JsonUtils;
import com.liuyk.commonlib.widget.MyViewPager;
import com.liuyk.commonlib.widget.navigation.BottomNavigationBar;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页容器/也可以用在其它地方
 * <p>
 * Created by liuyakui on 2020/6/24
 */
public class HomeContainerView extends LinearLayout implements BottomNavigationBar.OnBottomTabClickListener, ViewPager.OnPageChangeListener {
    private MyViewPager mViewPager;
    private HomeContainAdapter mAdapter;
    private BottomNavigationBar mBottomNavigationBar;
    private NavigationTabAdapter mTabAdapter;

    public HomeContainerView(Context context) {
        this(context, null);
    }

    public HomeContainerView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HomeContainerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        final View view = LayoutInflater.from(getContext()).inflate(R.layout.home_container, this, false);
        mViewPager = view.findViewById(R.id.view_pager);
        mViewPager.addOnPageChangeListener(this);
        mBottomNavigationBar = view.findViewById(R.id.bottom_contain);
        addView(view);
    }

    public void init(FragmentManager fm, ArrayList<Fragment> fragments, String configName) {
        if (fm == null || fragments == null || fragments.isEmpty()) {
            return;
        }
        mViewPager.setOffscreenPageLimit(fragments.size());
        mAdapter = new HomeContainAdapter(fm);
        mAdapter.setFragments(fragments);
        mViewPager.setAdapter(mAdapter);

        mTabAdapter = new NavigationTabAdapter();
        List<TablebarData> tables = getTables(configName);
        if (!tables.isEmpty()) {
            mTabAdapter.setItems(tables);
            mBottomNavigationBar.setAdapter(mTabAdapter);
            mBottomNavigationBar.setCurrentItem(0);
            mBottomNavigationBar.setOnBottomTabClickListener(this);
        }
    }

    private List<TablebarData> getTables(String configName) {
        List<TablebarData> items = new ArrayList<>();
        List<TablebarData> tempItems = JsonUtils.parseJson(getContext(), configName, TablebarData[].class);
        if (tempItems == null) {
            return items;
        }
        for (TablebarData data : tempItems) {
            items.add(data);
        }
        return items;
    }

    public void setOnPageChangeListener(ViewPager.OnPageChangeListener onPageChangeListener) {
        mViewPager.addOnPageChangeListener(onPageChangeListener);
    }

    public ViewPager getViewPager() {
        return mViewPager;
    }

    public HomeContainAdapter getAdapter() {
        return mAdapter;
    }

    public BottomNavigationBar getBottomNavigationBar() {
        return mBottomNavigationBar;
    }

    @Override
    public void bottomTabSelect(int index) {
        mTabAdapter.clickItem(index);
        mViewPager.setCurrentItem(index, false);
    }

    public void setScrollEnable(boolean isScroll) {
        mViewPager.setScrollEnable(isScroll);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        mBottomNavigationBar.setCurrentItem(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
